package com.mymorningroutine;
import java.util.*;
import android.widget.*;
import com.mymorningroutine.obj.*;
import android.view.*;
import android.os.*;
import android.view.View.*;
import android.content.*;
import android.app.*;

public class ActivitySectionFragment extends RefreshableFragment
{

	@Override
	protected void setTextHelp(TextView textView)
	{
		this.textHelp = textView;
	}

	@Override
	protected TextView getTextHelp()
	{
		// TODO: Implement this method
		return textHelp;
	}
	


	

	/**
	 * The fragment argument representing the section number for this
	 * fragment.
	 */
	public static final String ARG_SECTION_NUMBER = "section_number";
	private ArrayList<com.mymorningroutine.obj.Activity> activitiesList;
	
	private Button createActivityButton;
	protected static TextView textHelp;
	protected static ListView listView;
	
	public ActivitySectionFragment()
	{
		super();
	}



	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
							 Bundle savedInstanceState)
	{
		
		
		rootView = inflater.inflate(R.layout.fragment_activity, null);
		createActivityButton = (Button) rootView.findViewById(R.id.button_add_activity);
		createActivityButton.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					Class<?> itemActivity = ActivityDetailActivity.class;
					Intent intent = new Intent(getActivity(), itemActivity);
					startActivity(intent);
				}


			});

		
		textHelpId = R.id.text_help_activities;
		initiateViews();
		loadActivities();

		listView.setAdapter(new ActivitiesAdapter(getActivity(), R.layout.list_goal_item, activitiesList));
		listView.setLongClickable(true);
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
				@Override
				public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
				{
					LinearLayout layout = (LinearLayout)view;
					layout = (LinearLayout)layout.getChildAt(0);
					TextView item = (TextView) layout.getChildAt(0);
					Integer itemId = item.getId();

					Class<?> itemActivity = ActivityDetailActivity.class;
					Intent intent = new Intent(getActivity(), itemActivity);

					intent.putExtra("activityId", itemId);
					startActivity(intent);


				}
			});
		listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){

				@Override
				public boolean onItemLongClick(AdapterView<?> p1, View p2, int p3, long p4)

				{
					final com.mymorningroutine.obj.Activity activity = Singleton.getActivityTable(getActivity().getApplicationContext()).getActivity(p2.getId());

					AlertDialog. Builder alertDialogBuilder = new AlertDialog. Builder(getActivity());
// set title
					alertDialogBuilder. setTitle(getResources().getString(R.id.action_delete)) ;
// set dialog message
					alertDialogBuilder
						. setMessage(getResources().getString(R.id.action_delete) + "'" + activity.getTitle() + "'")
						. setCancelable(true)
						. setPositiveButton(getResources().getString(R.string.yes), new DialogInterface. OnClickListener() {
							public void onClick(DialogInterface dialog, int id)
							{
// if this button is clicked, close
// current activity
								//Delete activity
								Singleton.getActivityTable(getActivity().getApplicationContext()).deleteActivity(activity.getId());
							}
						})
						. setNegativeButton(getResources().getString(R.string.no) , new DialogInterface. OnClickListener() {
							public void onClick(DialogInterface dialog, int id)
							{
// if this button is clicked, just close
// the dialog box and do nothing
								dialog.cancel() ;
							}
						}) ;
// create alert dialog
					AlertDialog alertDialog = alertDialogBuilder.create();
// show it
					alertDialog.show();


					return true;
				}					
			});



		return rootView;
	}

	@Override
	protected void setListView(ListView inListView)
	{
		// TODO: Implement this method
		listView = inListView;
	}

	@Override
	protected ListView getListView()
	{
		// TODO: Implement this method
		return listView;
	}


	public void loadActivities()
	{
		activitiesList = Singleton.getActivityTable(getActivity()).getActivities();
		listView.setAdapter(new ActivitiesAdapter(getActivity(), R.layout.list_goal_item, activitiesList));
	}



	@Override
	public void refreshFragment()
	{
		// TODO: Implement this method
		loadActivities();
	}




}
